package cn.itcast.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  异常处理器
 */
public class SysExceptionResolver implements HandlerExceptionResolver {
    /**
     *  异常处理器业务逻辑
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        // 1.获取到异常对象
        SysException exception = null;

        if (ex instanceof SysException) {
            exception = (SysException)ex;
        } else {
            exception = new SysException("系统正在维护...");
        }

        // 2.ModelAndView对象,封装数据跳转视图
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("error", exception.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
